import {
  EMPLOYEE_NO_STORE_NAME,
  DEVICE_STORE_NAME,
  USER_STORE_NAME,
  USER_STATS_NAME,
} from "@/config/constants";

/**
 * 获取缓存的 employee number
 */
export function getEmployeeNo(): string | null {
  const employeeNo = sessionStorage.getItem(EMPLOYEE_NO_STORE_NAME);
  return employeeNo;
}

/**
 * 缓存 employee number
 *
 * @param employeeNo employeeNo
 * */
export function setEmployeeNo(employeeNo?: string) {
  removeEmployeeNo();
  if (employeeNo) {
    sessionStorage.setItem(EMPLOYEE_NO_STORE_NAME, employeeNo);
  }
}

/**
 * 移除 employee number
 */
export function removeEmployeeNo() {
  sessionStorage.removeItem(EMPLOYEE_NO_STORE_NAME);
}

/**
 * 获取缓存的 device info
 */
export function getDeviceInfo(): any {
  const info = sessionStorage.getItem(DEVICE_STORE_NAME);

  return JSON.parse(info);
}

/**
 * 缓存 device info
 * @param token token
 * */
export function setDeviceInfo(info?: object) {
  removeDeviceInfo();
  if (info) {
    sessionStorage.setItem(DEVICE_STORE_NAME, JSON.stringify(info));
  }
}

/**
 * 移除 device info
 */
export function removeDeviceInfo() {
  sessionStorage.removeItem(DEVICE_STORE_NAME);
}

/**
 * 获取缓存的 username
 */
export function getUsername(): any {
  const info = sessionStorage.getItem(USER_STORE_NAME);

  return info;
}

/**
 * 缓存 username
 * @param token token
 * */
export function setUsername(name: string) {
  removeUsername();
  if (name) {
    sessionStorage.setItem(USER_STORE_NAME, name);
  }
}

/**
 * 移除 username
 */
export function removeUsername() {
  sessionStorage.removeItem(USER_STORE_NAME);
}

/**
 * 获取缓存的 user stats 2022
 */
export function getUserStats(): any {
  const info = localStorage.getItem(USER_STATS_NAME);
  return info ? JSON.parse(info) : null;
}

/**
 * 缓存 user stats 2022
 * @param token token
 * */
export function setUserStats(stats: object) {
  removeUserStats();
  if (stats) {
    localStorage.setItem(USER_STATS_NAME, JSON.stringify(stats));
  }
}

/**
 * 移除 user stats 2022
 */
export function removeUserStats() {
  localStorage.removeItem(USER_STATS_NAME);
}
